﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FSY.API.Utilities
{
    public class Fsy_ConvertUtil
    {
        /// <summary>
        /// Chuyen mot doi tuong sang kieu ngay theo chuoi Format
        /// </summary>
        /// <param name="obj">Doi tuong can convert</param>
        /// <param name="strFormat">Kieu dinh dang muon convert</param>
        /// <returns></returns>
        public static DateTime ToDateTime(object obj, string strFormat)
        {
            try
            {
                string dtString = Fsy_Null.SetNullString(obj);
                string[] arr = new string[2];
                DateTime dt = DateTime.MinValue;

                if (dtString.IndexOf("/") > -1)
                {
                    arr = dtString.Split('/');
                }
                else if (dtString.IndexOf("-") > -1)
                {
                    arr = dtString.Split('-');
                }
                else
                {
                    return dt;
                }

                switch (strFormat)
                {
                    case "dd/mm/yyyy":
                    case "dd-mm-yyyy":
                        dt = Fsy_Null.SetNullDateTime(arr[1] + "/" + arr[0] + "/" + arr[2]);
                        break;
                    case "yyyy/mm/dd":
                    case "yyyy-mm-dd":
                        dt = Fsy_Null.SetNullDateTime(arr[1] + "/" + arr[2] + "/" + arr[0]);
                        break;
                    case "yyyy/dd/mm":
                    case "yyyy-dd-mm":
                        dt = Fsy_Null.SetNullDateTime(arr[2] + "/" + arr[1] + "/" + arr[0]);
                        break;
                    default:
                        dt = Fsy_Null.SetNullDateTime(obj);
                        break;
                }
                return dt;
            }
            catch
            {
                return DateTime.MinValue;
            }
        }

        /// <summary>
        /// Chuyen mot kieu DateTime ve mot kieu string
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="strFormat"></param>
        /// <returns></returns>
        public static string ToDateTimeString(DateTime dt, string strFormat)
        {
            string dtString;
            string strDay = Fsy_Null.SetNullString(dt.Day).PadLeft(2, '0');
            string strMonth = Fsy_Null.SetNullString(dt.Month).PadLeft(2, '0');
            string strYear = Fsy_Null.SetNullString(dt.Year);
            try
            {
                switch (strFormat)
                {
                    case "dd/mm/yyyy":
                    case "dd-mm-yyyy":
                        dtString = strDay + "/" + strMonth + "/" + strYear;
                        break;
                    case "yyyy/mm/dd":
                    case "yyyy-mm-dd":
                        dtString = strYear + "/" + strMonth + "/" + strDay;
                        break;
                    case "yyyy/dd/mm":
                        dtString = strYear + "/" + strDay + "/" + strMonth;
                        break;
                    default:
                        dtString = "";
                        break;
                }
                return dtString;
            }
            catch
            {
                return "";
            }
        }      
    }
}
